<?php
namespace App\Api\Controllers\LakalaUnion;


use Illuminate\Support\Facades\Log;

class BaseController
{
    static $config, $instance, $data;

//    const MAIN_URL ='https://test.wsmsd.cn/sit/jhzf/papi/';
    const MAIN_URL = 'https://papi.yufengfintech.com/papi/';
    const QR_SUBMIT = 'dynamicqrurl';

    const SCAN_PAY = 'micropay';

    const QUERY_ORDER = 'query';

    const REFUND = 'refund';



    public static function getInstance ()
    {
        if (! isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function params ($body, $mchId, $key)
    {
        $params['head'] = [
            'version' => '1.0',
            'inputCharset' => 'UTF-8',
            'merchantNo' => $mchId,
        ];
        $params['body'] = $body;
        $params['head']['sign'] = $this->createSign( array_merge($params['head'],$params['body']), $key);
        $params['head']['signType'] = 'MD5';
        self::$data = ['requestJson' => json_encode($params, JSON_UNESCAPED_UNICODE)];

        return $this;
    }

    /**
     * ASCII排序
     * @param array $params
     * @param $key
     * @return bool|string
     */
    public function createSign ($params = array(), $key)
    {
        if (!empty($params)) {
            $params = array_filter($params);
            $p = ksort($params);
            if ($p) {
                $str = '';
                foreach ($params as $k => $val) {
                    $val = trim($val);
                    if ($val === '' || $val === null) {
                        continue;
                    }
                    $str .= $k . '=' . $val . '&';
                }
                $strs = rtrim($str, '&');
                $stringSignTemp = $strs . $key;
                return md5($stringSignTemp);
            }
        }
        return false;
    }

    public function verify ($data, $key)
    {
        $sign = $data['head']['sign'];
        unset($data['head']['sign']);
        unset($data['head']['signType']);
        unset($data['body']['wxUniqueInfo']);
        unset($data['body']['aliUniqueInfo']);
        unset($data['body']['ysfUniqueInfo']);

        $sign1 = $this->createSign( array_merge($data['head'],$data['body']), $key);
        if ($sign == $sign1) {
            return true;
        }
        return false;
    }




    public function build ($url, $fun)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::MAIN_URL.$url); //设置选项，包括URL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);


        curl_setopt($ch, CURLOPT_POSTFIELDS, self::$data);

        $headers = array();
        $headers[] = 'Content-Type=multipart/form-data';
        $headers[] = 'Charset: UTF-8';
//        $headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $output = curl_exec($ch); //执行并获取HTML文档内容

        curl_close($ch); //释放curl句柄
        Log::channel("lakalaunion")->info($fun."_Request", self::$data);
        $output = json_decode($output, true);
        Log::channel("lakalaunion")->info($fun."_Response", $output);
        return $output;

    }

}